<html>

<head>
    <meta charset="utf-8">
    <title>编辑<?php print $name; ?></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
    <link rel="stylesheet" href="/theme/admin/assets/css/index.css">
    <link rel="stylesheet" href="/theme/admin/assets/layui/css/layui.css">
    <script src="/theme/admin/assets/js/lib/jquery.min.js"></script>
</head>

<body>
    <div class="pd-20 <?php print $type; ?>-edit">
        <form class="layui-form" action="">
          <div class="layui-form-item">
              <label class="layui-form-label">标题</label>
              <div class="layui-input-block">
                  <input type="text" name="title" lay-verify="title" value="<?php print '<?php echo $'.$type.'->title; ?>'; ?>" class="layui-input">
              </div>
          </div>
          <?php foreach ($fields as $field): ?>
            <?php if($field['html_type'] == 'select'): ?>

              <div class="layui-form-item">
                  <label class="layui-form-label"><?php print $field['lable']; ?></label>
                  <div class="layui-input-block">
                    <select name="<?php echo $field['name']; ?>">
                    <?php foreach ($field['html_type_option'] as $h):?>
              <option value="<?php echo $h['value']; ?>"><?php echo $h['lable']; ?></option>
              <?php endforeach;?>
                    </select>
                  </div>
              </div>
            <?php elseif($field['html_type'] == 'textarea'): ?>

              <div class="layui-form-item">
                  <label class="layui-form-label"><?php print $field['lable']; ?></label>
                  <div class="layui-input-block">
                      <textarea id="<?php print $field['name']; ?>" name="<?php print $field['name']; ?>" lay-verify="<?php print $field['name']; ?>_content" style="display: none;"><?php print '<?php echo $'.$type.'->'.$field['name'].'; ?>'; ?></textarea>
                  </div>
              </div>
            <?php elseif($field['html_type'] == 'checkbox'): ?>

              <div class="layui-form-item">
                  <label class="layui-form-label"><?php print $field['lable']; ?></label>
                  <div class="layui-input-block">
                    <?php foreach ($field['html_type_option'] as $h):?>
                        <input type="checkbox" name="<?php echo $field['name']; ?>[<?php echo $h['value']; ?>]" title="<?php echo $h['lable']; ?>">
                    <?php endforeach;?>
                  </div>
              </div>
            <?php elseif($field['html_type'] == 'radio'): ?>

              <div class="layui-form-item">
                  <label class="layui-form-label"><?php print $field['lable']; ?></label>
                  <div class="layui-input-block">
                      <?php foreach ($field['html_type_option'] as $h):?>
                          <input type="radio" name="<?php echo $field['name']; ?>" value="<?php echo $h['value']; ?>" title="<?php echo $h['lable']; ?>">
                      <?php endforeach;?>
                  </div>
              </div>
            <?php elseif($field['html_type'] == 'file' || $field['html_type'] == 'image'): ?>

              <div class="layui-form-item">
                  <label class="layui-form-label"><?php print $field['lable']; ?></label>
                  <div class="layui-input-block">
                    <input type="text" name="<?php echo $field['name']; ?>" value="<?php print '<?php echo $'.$type.'->'.$field['name'].'; ?>'; ?>" class="layui-input layui-input-inline" id="<?php echo $field['name']; ?>">
                    <div class="layui-box layui-upload-button">
                      <input type="file" name="file"<?php echo $field['html_type'] == 'file' ? ' lay-type="file"' : ''; ?> class="layui-upload-file-<?php echo $field['name']; ?>">
                    </div>
                  </div>
              </div>

            <?php elseif($field['html_type'] == 'hidden'): ?>

              <input type="<?php print $field['html_type']; ?>" name="<?php print $field['name']; ?>" value="1">
            <?php else: ?>

              <div class="layui-form-item">
                  <label class="layui-form-label"><?php print $field['lable']; ?></label>
                  <div class="layui-input-block">
                      <input type="<?php print $field['html_type']; ?>" name="<?php print $field['name']; ?>" value="<?php print '<?php echo $'.$type.'->'.$field['name'].'; ?>'; ?>" class="layui-input">
                  </div>
              </div>
            <?php endif; ?>
          <?php endforeach; ?>

          <input type="hidden" name="<?php print substr($type, 0, 1 ); ?>id" value="<?php print '<?php echo $'.substr($type, 0, 1 ); ?>id; ?>" />
          <div class="layui-form-item">
              <div class="layui-input-block submit">
                  <button class="layui-btn" lay-submit="" lay-filter="<?php print $type; ?>update">提交</button>
              </div>
          </div>
        </form>
    </div>
    <script src="/theme/admin/assets/layui/layui.js"></script>
    <script src="/theme/admin/assets/js/define/common.js"></script>
    <script>
        layui.use(['form', 'layedit', 'layer', 'upload'], function() {
            var form = layui.form();
            var layedit = layui.layedit;
            <?php foreach ($fields as $field): ?>
              <?php if($field['html_type'] == 'textarea'): ?>

              var <?php print $field['name']; ?>_index = layedit.build('<?php print $field['name']; ?>',{
                uploadImage: {
              		url:'/admin/<?php print $type; ?>/<?php print $field['name']; ?>/upload'
              		,type: 'post'
                }
        			}); //建立编辑器

              form.verify({
                 <?php print $field['name']; ?>_content: function(value) {
                     return layedit.sync(<?php print $field['name']; ?>_index);
                 }
              });

              <?php elseif($field['html_type'] == 'image' || $field['html_type'] == 'file'): ?>

              layui.upload({
                url: '/admin/<?php print $type; ?>/<?php print $field['name']; ?>/upload',
                elem: '.layui-upload-file-<?php echo $field['name']; ?>',
                success: function (result) {
                    document.getElementById('<?php print $field['name']; ?>').value = result.filename;
                }
              });
              <?php endif; ?>
            <?php endforeach; ?>

            //监听提交
            form.on('submit(<?php print $type; ?>update)', function(data) {
                $.ajax({
                    type: "POST",
                    url: "/admin/<?php print $type; ?>/update", //后台程序地址
                    data: data.field, //需要post的数据
                    success: function(msg) { //后台程序返回的标签，比如我喜欢使用1和0 表示成功或者失败
                        var index = parent.layer.getFrameIndex(window.name);
                        if (!msg) {
                            layer.msg('更新失败', {
                                icon: 5
                            });

                            parent.layui.jquery("#iframe_status").val('0');
                            parent.layer.close(index);
                        } else {
                            parent.layui.jquery("#iframe_status").val('1');
                            parent.layer.close(index);
                        }
                    }
                });
                return false;
            });
        });
    </script>
</body>

</html>
